/* 
 * File:   LocalSource.h
 */
#ifndef LOCALSOURCE_H
#define	LOCALSOURCE_H

#include <dirent.h>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdlib.h>
#include <cstdlib>
#include <fstream>
#include <vmime/vmime.hpp>
#include <vmime/platforms/posix/posixHandler.hpp>
#include <vector>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <sys/stat.h>

#include "File.h"
#include "Log.h"
#include "LocalAdress.h"
#include "Account.h"

class LocalSource {
public:

    std::vector<File>& getLocalFiles();

    static bool FileExists(string& strFilename) {
        struct stat stFileInfo;
        bool blnReturn;
        int intStat;
        intStat = stat(strFilename.c_str(), &stFileInfo);
        if (intStat == 0) {
            blnReturn = true;
        } else {
            blnReturn = false;
        }

        return (blnReturn);
    };

    /*
    static void archiveFileBeforeSent(File& file_)
    {
        string buf = file_.full_local_path;
        file_.full_local_path = file_.full_local_path+".tgz";

        Log::getInstance()->sDebugging("\t\tarchiveFileBeforeSent");
        Log::getInstance()->sDebugging("\t\tNew full local path "+file_.full_local_path);
        chdir(file_.path_only.c_str());
        string s = ("tar -czf "+file_.name+".tgz "+file_.name);//+" 2> /dev/null");
        system(s.c_str());
    };
    static void deleteArchiveAfterSent(File& file_)
    {
        remove(file_.full_local_path.c_str());
    };

    static void archiveFileAfterReceive(File* file_, string buf_)
    {
        Log::getInstance()->sDebugging("\t\tarchivefileAfterReceive");
        Log::getInstance()->sDebugging("\t\tfile_.full_local_path"+file_->full_local_path);
        Log::getInstance()->sDebugging("\t\tdist "+buf_);

        chdir(file_->path_only.c_str());
        char curdir[1000];
        getcwd (curdir, sizeof (curdir));
        string ss(curdir);
        Log::getInstance()->sDebugging("");
        Log::getInstance()->sDebugging(file_->path_only);
        Log::getInstance()->sDebugging(ss);
        Log::getInstance()->sDebugging("tar -xzf "+file_->name+".tgz");
        Log::getInstance()->sDebugging("");
        string s = ("tar --keep-newer-files -xzf "+file_->name+".tgz");//+" "+buf_);//+" 2>/dev/null");
        system(s.c_str());
       remove(file_->full_local_path.c_str());
    };
*/
    void preCleaning(std::vector<File>& local_files_);
    void preArchivingStuff(std::vector<File>& local_files_, std::vector<File>& remote_files_);
    void postArchivingStuff(std::vector<File>& local_files_, std::vector<File>& remote_files_);
    void Connect();

    string getLocalPath()
    {
        return loc_dir;
    };

    LocalSource(LocalAdress& local_adress_ ,bool if_archive_flag_, bool if_synch_, string synch_) : local_adress(local_adress_), if_archive_flag (if_archive_flag_), if_synch(if_synch_), synch(synch_) {};

private:
    void doDir(std::string& s);
    bool if_synch;
    bool if_dir;
    bool if_archive_flag;
    LocalAdress& local_adress;
    string loc_dir;
    string synch;
    std::vector<File> local_files;
    DIR* dir;

};

#endif	/* LOCALSOURCE_H */

